package com.yupi.usercenter.exception;

import com.yupi.usercenter.commons.BaseResponse;
import com.yupi.usercenter.commons.ErrorCode;
import com.yupi.usercenter.commons.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 麦翔盛
 * @ClassName: GlobalExceptionHandler
 * @Package: com.yupi.exception
 * @description: 全局异常处理类
 * @date 2022/4/13 15:56
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理 BusinessException 错误
     * @param e 错误
     * @return 处理
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
        log.error("businessException", e.getMessage(),e);
        return ResponseUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }

    /**
     * 处理运行时异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e){
        log.error("RuntimeException", e.getMessage(),e);
        return ResponseUtils.error(ErrorCode.SYSTEM_ERROR.getCode(),e.getMessage(),"");
    }
}
